package titan.wizards.newTemplateComponent;

import java.io.File;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import titan.messages.Messages;
import titan.utils.FileUtils;
import titan.utils.TitanUtils;
import titan.wizards.BaseTitanPage;

/**
 * At this page you can define the name of the new component to be created
 */
public class NewComponentPage extends BaseTitanPage {
	private Text unixName;
	private IStatus pageStatus;
	private String componentPath;
	//Status
	private IStatus unixNameStatus;

	protected NewComponentPage() {
		super(Messages.NewComponent);
		setTitle(Messages.NewComponent);
		setDescription(Messages.NewComponentPage_desc);
		unixNameStatus = pageStatus = new Status(IStatus.OK, "default", ""); //$NON-NLS-1$ //$NON-NLS-2$
	}

	/**
	 * {@inheritDoc}
	 */
	public void createControl(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout(4, false);
		layout.verticalSpacing = 6;
		composite.setLayout(layout);
		setControl(composite);
		GridData gd = new GridData(320, 13);

		MouseTrackAdapter mouseExit = new MouseTrackAdapter() {
			public void mouseExit(MouseEvent e) {
			    applyStatus(pageStatus);
			}
		};

		new Label(composite, SWT.NONE).setText(Messages.UnixName+":"); //$NON-NLS-1$
		unixName = new Text(composite, SWT.BORDER | SWT.SINGLE);
		gd.horizontalSpan = 2;
		gd.horizontalAlignment = SWT.FILL;
		unixName.setLayoutData(gd);
		unixName.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent event) {
				Status status = new Status(IStatus.OK, "projectName", ""); //$NON-NLS-1$ //$NON-NLS-2$
				try {
					if(!unixName.getText().isEmpty()) //$NON-NLS-1$
						if(!FileUtils.isEmptyFolder(componentPath+unixName.getText()))
							status = new Status(IStatus.WARNING, "unixName", //$NON-NLS-1$
									Messages.CompFolderNEmpty+Messages.ContentWillBeReplaced);
				} catch (Exception e) {
					status = new Status(IStatus.ERROR, "unixName", e.getMessage()); //$NON-NLS-1$
				}
				applyStatus(status);
				updateButtons();
			}
		});
		unixName.addListener(SWT.MouseEnter, new Listener () {
			public void handleEvent(Event e) {
				applyStatus(new Status(IStatus.INFO, "packageName", Messages.NewComponentName_desc) ); //$NON-NLS-1$
			}
		});
		unixName.addMouseTrackListener(mouseExit);
	}

	/**
	 * Try to get project component folder path, where component will be created
	 * @param project - the project to get property
	 * @throws Exception if it couldn't get property
	 */
	public void init(IProject project) throws Exception {
		componentPath = TitanUtils.getProjectProperty(project, "projectPath") //$NON-NLS-1$
				+"local"+File.separator+"component"+File.separator; //$NON-NLS-1$ //$NON-NLS-2$
	}

	/**
	 * Copy the template component from plugin resources to application directory
	 * @return true if everything went OK, false otherwise
	 */
	public boolean createComponent(IProject project) {
		try {
			//copy template component from resources folder to the component folder
			File src = FileUtils.getResourceFile("/resources/component"); //$NON-NLS-1$
			File dest  = new File(componentPath+unixName.getText());
			FileUtils.copyAllFiles(src, dest);
		} catch (Exception e) {
			MessageDialog.openError(getShell(), Messages.Error, e.getMessage());
			return false;
		}

		try {
			project.refreshLocal(IResource.DEPTH_INFINITE, null);
		} catch (Exception e) {
			//do nothing... unable to refresh project tree (silent fail)
		}
		return true;
	}

	/**
	 * {@inheritDoc}
	 */
	protected void applyStatus(IStatus new_status) {
		if(new_status.getSeverity() != IStatus.INFO)
			unixNameStatus = new_status;
		applyPageStatus(new_status);
	}

	/**
	 * {@inheritDoc}
	 */
	protected IStatus greaterStatus() {
		return unixNameStatus;
	}

	/**
	 * Returns whether this page is completed
	 * @return true if the wizard could be finished, and false otherwise
	 */
	public boolean canFinish() {
		if(pageStatus.getSeverity() == IStatus.ERROR || unixName.getText().isEmpty()) //$NON-NLS-1$
			return false;
		return true;
	}

	/**
	 * {@inheritDoc}
	 */
	public boolean canFlipToNextPage() {
		return false;
	}
}
